﻿#region Using
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Web;
using System.Web.Mvc;

using Mvp.Library.Entities;
using Mvp.Mvc.ViewModels;
using Mvp.Mvc.Models;
using Mvp.Mvc.Models.Google;
#endregion // System

namespace Mvp.Mvc.Controllers
{
    public class HomeController : BaseController
    {
        public ActionResult Index()
        {
            return View(new HomeViewModel()
                        {
                            SearchQuery = string.Empty,
                            //                Announcements = this.Context.Announcements
                            //                                    .Where(a => a.IsActive)
                            //                                    .OrderByDescending(a => a.CreatedOn)
                            //                                    .Take(5)
                            //                                    .ToList(),
                            Announcements = new List<Announcement>()
                                            {
                                                new Announcement()
                                                {
                                                    Title = "New Website",
                                                    Text = "As you can see, we've redesigned the website and added functionality to serve the community better.  We hope you find the new website informative and helpful.",
                                                    CreatedOn = DateTime.Parse("10/26/2011 10:45 AM"),
                                                    CreatedBy = new User()
                                                                {
                                                                    UserName = "spercy"                                                                
                                                                }
                                                },
                                                new Announcement()
                                                {
                                                    Title = "Test Announcement",
                                                    Text = "This is a test announcement.",
                                                    CreatedOn = DateTime.Parse("10/16/2011 1:25 PM"),
                                                    CreatedBy = new User()
                                                                {
                                                                    UserName = "test user"                                                                
                                                                }
                                                }

                                            },
                            ContactUs = new ContactUsItem(),
                            ContactUsSubmitted = false
                        });
        }// of Index()

        //
        // POST: /Home/Search

        [HttpPost]
        public ActionResult Search(string searchQuery)
        {
            if (!string.IsNullOrWhiteSpace(searchQuery))
            {
                SearchResult result = new Models.Google.SearchResult();
                using (var web = new System.Net.WebClient())
                {
                    web.Headers.Add("Referrer", "http://www.mountvernonplantation.com/");
                    string json = web.DownloadString(string.Format(
                           "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=site:mountvernonplantation.com+{0}&ABQIAAAA7armP0-H8Rzl8OJxKY_zRxS1_GYQ-iVmROjdBV2jPXWJK0AOkRRO7r99wExho8M1sMeUbULWA-swKg",
                           searchQuery));
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(result.GetType());
                    using (MemoryStream stream = new MemoryStream(Encoding.Unicode.GetBytes(json)))
                    {
                        result = serializer.ReadObject(stream) as SearchResult;
                    }// using the memory stream
                }// if there is a search query
                result.SearchQuery = searchQuery;
                return this.View(result);
            }// if there is a search query
            else { return this.RedirectToAction("Index"); }
        }// of ActionResult Search(string)

        //
        // POST: /Home/Index

        [HttpPost]
        public ActionResult Index(HomeViewModel model)
        {
            model.ContactUsSubmitted = true;
            return base.View(model);
        }// of ActionResult Index(HomeViewModel)

        public ActionResult CommunityInfo()
        {
            return base.View();
        }// of CommunityInfo()

        public ActionResult NeighborhoodInfo()
        {
            return base.View();
        }// of NeighborhoodInfo()

        public ActionResult RealEstateInfo()
        {
            IList<RealEstateItem> realEstateItems = new List<RealEstateItem>()
                                {
                                    new RealEstateItem()
                                    {
                                        UnitNumber = 24,
                                        NumberOfBedrooms = 2,
                                        NumberOfBathrooms = 2.5,
                                        Price = 159000,
                                        Contact = "Mark Spain, Keller Williams Realty North Atlanta, 770-663-7291",
                                        Type = RealEstateItem.ItemType.ForSale,
                                        IsActive = true
                                    },
                                    new RealEstateItem()
                                    {
                                        UnitNumber = 60,
                                        NumberOfBedrooms = 2,
                                        NumberOfBathrooms = 2.5,
                                        Price = 120000,
                                        Contact = "Anna Kilinski, Keller Williams Realty Intown, 404-541-3500",
                                        Type = RealEstateItem.ItemType.ForSale,
                                        IsActive = true
                                    },
                                    new RealEstateItem()
                                    {
                                        UnitNumber = 200,
                                        NumberOfBedrooms = 3,
                                        NumberOfBathrooms = 2.5,
                                        Price = 1200,
                                        Contact = "John Smith",
                                        Type = RealEstateItem.ItemType.ForLease,
                                        IsActive = true
                                    },
                                    new RealEstateItem()
                                    {
                                        UnitNumber = 103,
                                        NumberOfBedrooms = 2,
                                        NumberOfBathrooms = 2.5,
                                        IncludesGarage = true,
                                        Price = 189000,
                                        Contact = "Aziz Mohammadi, Solid Source Realty, 770-475-1130",
                                        Type = RealEstateItem.ItemType.ForSale,
                                        IsActive = true
                                    },
                                    new RealEstateItem()
                                    {
                                        UnitNumber = 150,
                                        NumberOfBedrooms = 2,
                                        NumberOfBathrooms = 2.5,
                                        Price = 950,
                                        Contact = "Bob Smith",
                                        Type = RealEstateItem.ItemType.ForLease,
                                        IsActive = true
                                    }
                                };
            //return base.View(this.Context.RealEstateItems
            return base.View(realEstateItems
                                            .Where(r => r.IsActive)
                                            .OrderBy(r => r.UnitNumber)
                                            .ToList());
        }// of RealEstateInfo()
    }// of class HomeController
}// of namespace Mvp.Mvc.Controllers